<?php

namespace app\store\service;

use app\store\model\Coupon;
use think\facade\Db;

class CouponService
{
    public function getCouponList($keyword)
    {   
        $store_id = session('store_id');
        
        $model = new Coupon();
        $query = $model->field('id,coupon_type,coupon_name,icon,quota,grant_count,used_amount,expire_type,expire_day,start_time,
                       end_time,status,create_time,update_time')
                       ->withAttr('coupon_type',function ($value){
                            $type = [1 => '满减卷',2 => '立减劵',3 => '拼团卷', 4 => '转卖卷'];
                            return $type[$value];
                       })->where('store_id',$store_id);

        if (!empty($param['coupon_type'])){
            $query->where('coupon_type',$param['coupon_type']);
        }
        
        if(!empty($param['keyword'])){
            $query->whereLike('coupon_name',"%{$keyword}%");
        }

        $coupons = $query->order('id','desc')->paginate(10);
        
        return  $coupons;
        
    }

    public function findCoupon($id)
    {
        $coupon = Db::name('coupon')->withOutField('create_time,update_time')->where('id',$id)->find();

        $cols = Db::name('goods')
                    ->alias('a')
                    ->field('a.id goods_id,a.goods_name,a.main_image')
                    ->join('coupon_goods b','a.id=b.goods_id')
                    ->where('b.coupon_id',$id)
                    ->select();
        $goods = [];

        if (!$cols->isEmpty()){
            $goods = $cols->toArray();
        }

        $coupon['goods'] = $goods;
    
        return $coupon;
        
    }


    public static function saveCoupon($data)
    {

        if (empty($data['start_time']) || empty($data['end_time'])){
            unset($data['start_time'],$data['end_time']);
        }

        $coupon = Coupon::create($data);



        $coupon_id = $coupon['id'];

        if(!empty($data['allow_goods'])){
            $allow = json_decode($data['allow_goods'],true);
            $arr = [];
            foreach($allow as $k => $v){
                $arr[$k]['goods_id'] = $v['goods_id'];
                $arr[$k]['coupon_id'] = $coupon_id; 
            }

            Db::name('coupon_goods')->insertAll($arr);
        }

        return $coupon_id;
    }


    public static function updateCoupon($data)
    {

        if (empty($data['start_time']) || empty($data['end_time'])){
            unset($data['start_time'],$data['end_time']);
        }
    
        $coupon = Coupon::update($data);
        
        $coupon_id = $coupon['id'];

        if(!empty($data['allow_goods'])){
            $allow = json_decode($data['allow_goods'],true);
            $arr = [];
            foreach($allow as $k => $v){
                $arr[$k]['goods_id'] = $v['goods_id'];
                $arr[$k]['coupon_id'] = $coupon_id;
            }
            Db::transaction(function () use($coupon_id,$arr){
                Db::name('coupon_goods')->where('coupon_id',$coupon_id)->delete();
                Db::name('coupon_goods')->insertAll($arr);
            });
        }

        return $coupon_id;
    }


    public function addGoodsCoupon($allow_goods)
    {
        if(!empty($data['allow_goods'])){
            $allow = json_decode($data['allow_goods'],true);
            $arr = [];
            foreach($allow as $k => $v){
                $arr[$k]['goods_id'] = $v['goods_id'];
                $arr[$k]['coupon_id'] = $coupon_id;
            }
            Db::name('coupon_goods')->insertAll($arr);
        }
    }
    
}